package com.shengsheng.expschedule.service.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengsheng.expschedule.base.result.ResultCodeEnum;
import com.shengsheng.expschedule.service.base.exception.ExpScheduleException;
import com.shengsheng.expschedule.service.ucenter.entity.Right;
import com.shengsheng.expschedule.service.ucenter.entity.Role;
import com.shengsheng.expschedule.service.ucenter.entity.Teacher;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.RightVo;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.RoleInfoVo;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.RoleNameVo;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.RoleVo;
import com.shengsheng.expschedule.service.ucenter.mapper.RightMapper;
import com.shengsheng.expschedule.service.ucenter.mapper.RoleMapper;
import com.shengsheng.expschedule.service.ucenter.mapper.TeacherMapper;
import com.shengsheng.expschedule.service.ucenter.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 会员角色表 服务实现类
 * </p>
 *
 * @author yuan
 * @since 2021-11-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RightMapper rightMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public boolean saveByRoleVo(RoleInfoVo roleVo) {

        // 判断该角色名是否已经存在
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name", roleVo.getRoleName())
                .select("id");
        if (baseMapper.selectOne(roleQueryWrapper) != null)
            throw new ExpScheduleException(ResultCodeEnum.ROLE_SAVE_ERROR);
        Role role = new Role();
        BeanUtils.copyProperties(roleVo, role);
        return this.save(role);
    }

    @Override
    public boolean assignRightsByRoleId(String id, String rightIds) {

        // 判断是不是超级管理员或普通用户，该类角色不允许修改
        if ("1456256570730008578".equals(id) || "1457924563574747137".equals(id)) throw new ExpScheduleException(ResultCodeEnum.ROLE_IDENTITY_ERROR);
        Role role = baseMapper.selectById(id);
        role.setRoleRightIds(rightIds);
        return this.updateById(role);
    }

    @Override
    public List<RoleVo> getRoleList() {

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.select("id", "role_name", "role_right_ids");
        // 所有角色数据
        List<Role> roleList = this.list(roleQueryWrapper);
        // 组装 List<RoleVo>
        List<RoleVo> roleVoList = new ArrayList<>();
        for (Role role : roleList) {
            // 组装 RoleVo 对象
            RoleVo roleVo = new RoleVo();
            BeanUtils.copyProperties(role, roleVo);
            // 根据 id 获得对应的权限 RightVo 数据
            List<RightVo> rightList = getRightListById(role.getId());
            roleVo.setRightList(rightList);
            roleVoList.add(roleVo);
        }
        return roleVoList;
    }

    @Override
    public boolean removeRoleById(String id) {

        // 判断是不是超级管理员或普通用户，该类角色不允许删除
        if ("1456256570730008578".equals(id) || "1457924563574747137".equals(id)) throw new ExpScheduleException(ResultCodeEnum.ROLE_IDENTITY_ERROR);
        // 判断此角色是否已经被拿个老师使用
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("role_id", id).select("id");
        if (teacherMapper.selectOne(teacherQueryWrapper) != null)
            throw new ExpScheduleException(ResultCodeEnum.ROLE_DELETE_ERROR);
        // 不为超级管理员，可以删除
        return this.removeById(id);
    }

    @Override
    public boolean updateAdminRole(String rightId) {

        // 获取到超级管理员
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("id", "1456256570730008578");
        Role role = baseMapper.selectOne(roleQueryWrapper);
        // 将新增权限同步添加到超级管理员权限中
        if (role.getRoleRightIds().equals("")) role.setRoleRightIds(rightId);
        else role.setRoleRightIds(role.getRoleRightIds() + "," + rightId);
        return this.updateById(role);
    }

    @Override
    public List<RightVo> getRightListById(String roleId) {

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("id", roleId)
                .select("role_right_ids");
        Role role = baseMapper.selectOne(roleQueryWrapper);
        String rightIds = role.getRoleRightIds();
        String[] rightArray = rightIds.split(",");
        // 查询所有的权限信息
        QueryWrapper<Right> rightQueryWrapper = new QueryWrapper<>();
        rightQueryWrapper.in("id", Arrays.asList(rightArray))
                .orderByAsc("sort")
                .select("id", "right_name", "right_parent_id", "right_path", "right_icon");
        List<Right> rightList = rightMapper.selectList(rightQueryWrapper);
        // 组装 RightVo
        List<RightVo> rightVoArrayList = new ArrayList<>();
        for (Right right : rightList) {
            RightVo rightVo = new RightVo();
            BeanUtils.copyProperties(right, rightVo);
            List<RightVo> rightVos = rightMapper.selectNestedListByParentId(right.getId());
            if (rightVos.size() > 0) rightVo.setChildren(rightVos);
            rightVoArrayList.add(rightVo);
        }
        return rightVoArrayList;
    }

    @Override
    public List<RightVo> getRightMenuListById(String roleId) {

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("id", roleId)
                .select("role_right_ids");
        Role role = baseMapper.selectOne(roleQueryWrapper);
        String rightIds = role.getRoleRightIds();
        String[] rightArray = rightIds.split(",");
        // 查询所有的权限信息
        QueryWrapper<Right> rightQueryWrapper = new QueryWrapper<>();
        rightQueryWrapper
                .eq("right_is_show",1)
                .in("id", Arrays.asList(rightArray))
                .orderByAsc("sort")
                .select("id", "right_name", "right_parent_id", "right_path", "right_icon");
        List<Right> rightList = rightMapper.selectList(rightQueryWrapper);
        // 组装 RightVo
        List<RightVo> rightVoArrayList = new ArrayList<>();
        for (Right right : rightList) {
            RightVo rightVo = new RightVo();
            BeanUtils.copyProperties(right, rightVo);
            List<RightVo> rightVos = rightMapper.selectMenuNestedListByParentId(right.getId());
            if (rightVos.size() > 0) rightVo.setChildren(rightVos);
            rightVoArrayList.add(rightVo);
        }
        return rightVoArrayList;
    }

    @Override
    public String getRoleNameById(String roleId) {

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.select("role_name")
                .eq("id", roleId);
        return baseMapper.selectOne(roleQueryWrapper).getRoleName();
    }

    @Override
    public List<RoleNameVo> getRoleNameList() {

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.select("id", "role_name");
        List<Role> roleList = baseMapper.selectList(roleQueryWrapper);
        List<RoleNameVo> roleNameVoList = new ArrayList<>();
        for (Role role : roleList) {
            RoleNameVo roleNameVo = new RoleNameVo();
            BeanUtils.copyProperties(role, roleNameVo);
            roleNameVoList.add(roleNameVo);
        }
        return roleNameVoList;
    }

    @Override
    public String getRoleIdByRoleName(String roleName) {

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name", roleName)
                .select("id");
        return baseMapper.selectOne(roleQueryWrapper).getId();
    }

}
